/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using WIA;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using MyPhotoIndex.Utilities;

namespace MyPhotoIndex.Importer.CameraImageAcquisition
{
    public class DigitalCameraImage : IAcquiredImage
    {
        private ItemClass m_item;
        private Image m_thumbnail;
        private string m_name;
        private string m_fullName;
        private string m_fileExtention;
        private DateTime m_dateTaken;
        private int m_thumbnailHeight;
        private int m_thumbnailWidth;

        internal DigitalCameraImage(ItemClass item)
        {
            m_item = item;
            try
            {
                PropertyClass propertyClass = GetItemProperty("Item Name");
                m_name = (string)propertyClass.get_Value();

                propertyClass = GetItemProperty("Full Item Name");
                m_fullName = (string)propertyClass.get_Value();

                propertyClass = GetItemProperty("Filename extension");
                m_fileExtention = (string)propertyClass.get_Value();

                propertyClass = GetItemProperty("Item Time Stamp");
                VectorClass dateVectorClass = (VectorClass)Marshal.CreateWrapperOfType((IVector)propertyClass.get_Value(), typeof(VectorClass));
                m_dateTaken = dateVectorClass.Date;

                propertyClass = GetItemProperty("Thumbnail Width");
                m_thumbnailWidth = (int)propertyClass.get_Value();

                propertyClass = GetItemProperty("Thumbnail Height");
                m_thumbnailHeight = (int)propertyClass.get_Value();

                propertyClass = GetItemProperty("Thumbnail Data");
                VectorClass thumbnailVectorClass = (VectorClass)Marshal.CreateWrapperOfType((IVector)propertyClass.get_Value(), typeof(VectorClass));
                ImageFile imageFile = thumbnailVectorClass.get_ImageFile(m_thumbnailWidth, m_thumbnailHeight);
                byte[] byteArray = (byte[])imageFile.FileData.get_BinaryData();
                m_thumbnail = ThumbnailUtilities.ThumbnailFromByteArray(byteArray);
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
        }

        internal PropertyClass GetItemProperty(string propertyName)
        {
            object o = propertyName;
            Property p = m_item.Properties.get_Item(ref o);
            PropertyClass propertyClass = (PropertyClass)Marshal.CreateWrapperOfType((IProperty)p, typeof(PropertyClass));
            return propertyClass;
        }

        public void Transfer(string fileName)
        {
            ImageFile imageFile = (ImageFile)m_item.Transfer("{00000000-0000-0000-0000-000000000000}");
            imageFile.SaveFile(fileName);
        }

        public void Delete()
        {
            try
            {
                IWiaItem wiaItem = (IWiaItem)m_item.WiaItem;
                wiaItem.DeleteItem(0);
            }
            catch { }
        }

        public Image Thumbnail
        {
            get { return m_thumbnail; }
        }

        public string FullName
        {
            get { return m_fullName; }
        }

        public string Name
        {
            get { return m_name; }
        }

        public string FileExtention
        {
            get { return m_fileExtention; }
        }

        public int ThumbnailHeight
        {
            get { return m_thumbnailHeight; }
        }

        public int ThumbnailWidth
        {
            get { return m_thumbnailWidth; }
        }

        public DateTime DateTaken
        {
            get { return m_dateTaken; }
        }

        #region IAcquiredImage Members

        #endregion
    }



    [ComImport, Guid("4db1ad10-3391-11d2-9a33-00c04fa36145")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IWiaItem
    {
        void GetItemType(
            [Out] out int itemType);

        void AnalyzeItem(
            [In] int lTags);

        void EnumChildItems(
            [Out, MarshalAs(UnmanagedType.Interface)] out object iEnumWiaItem);

        void DeleteItem(
            [In] int lTags);

        void CreateChildItem(
            [In] int lTags,
            [In, MarshalAs(UnmanagedType.BStr)] string strItemName,
            [In, MarshalAs(UnmanagedType.BStr)] string strFullItemName,
            [Out, MarshalAs(UnmanagedType.Interface)] out object iWiaItem);

        void EnumRegisterEventInfo(
            [In] int lTags,
            [In] ref Guid eventGuid,
            [Out, MarshalAs(UnmanagedType.Interface)] out object iEnum);

        void FindItemByName(
            [In] int lTags,
            [In, MarshalAs(UnmanagedType.BStr)] string strFullItemName,
            [Out, MarshalAs(UnmanagedType.Interface)] out object iWiaItem);

        void DeviceDlg(
            [In] IntPtr hwndParent,
            [In] int lTags,
            [In] int lIntent,
            [Out] out int iItemCount,
            [Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 128)] IWiaItem[] iWiaItem);

        void DeviceCommand(
            [In] int lTags,
            [In] ref Guid cmdGuid,
            [In, Out, MarshalAs(UnmanagedType.Interface)] ref object iWiaItem);

        void GetRootItem(
            [Out, MarshalAs(UnmanagedType.Interface)] out object iWiaItem);

        void EnumDeviceCapabilities(
            [In] int lTags,
            [Out, MarshalAs(UnmanagedType.Interface)] out object iEnumWIA_DEV_CAPS);

        void DumpItemData(
            [Out, MarshalAs(UnmanagedType.BStr)] out string strData);

        void DumpDrvItemData(
            [Out, MarshalAs(UnmanagedType.BStr)] out string strData);

        void DumpTreeItemData(
            [Out, MarshalAs(UnmanagedType.BStr)] out string strData);

        void Diagnostic(
            [In] int nSize,
            [In] byte[] buffer);
    }
}
